package io.renren.util;

import java.security.MessageDigest;

/**
 * Created by schen on 1/20/16.
 */
public class DigestUtil {
    public static final String passsalt="21a57f2fe765e1ae4a8bf15d73fc1bf2a533f547f2343d12a499d9c0592044d4";

    public static String md5( String source ) {
        try {
            MessageDigest md = MessageDigest.getInstance( "MD5" );
            byte[] bytes = md.digest( (source+ passsalt).getBytes("UTF-8") );
            return getString( bytes );
        } catch( Exception e )	{
            e.printStackTrace();
            return null;
        }
    }

    private static String getString( byte[] bytes )
    {
        StringBuffer sb = new StringBuffer();
        for( int i=0; i<bytes.length; i++ )
        {
            byte b = bytes[ i ];
            String hex = Integer.toHexString((int) 0x00FF & b);
            if (hex.length() == 1)
            {
                sb.append("0");
            }
            sb.append( hex );
        }
        return sb.toString();
    }
}
